package com.ssm.sport.controller;

import com.ssm.sport.entity.Shipping;
import com.ssm.sport.entity.User;
import com.ssm.sport.service.ShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 收货地址控制器
 */
@Controller
@RequestMapping("/shipping")
public class ShippingController {

    @Autowired
    private ShippingService shippingService;

    /**
     * 添加收货地址
     */
    @PostMapping("/add")
    @ResponseBody
    public Map<String, Object> add(@RequestBody Shipping shipping, HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        
        // 从session中获取用户信息
        User user = (User) session.getAttribute("user");
        if (user == null) {
            result.put("status", 0);
            result.put("msg", "用户未登录");
            return result;
        }
        
        Integer shippingId = shippingService.add(user.getId(), shipping);
        if (shippingId != null) {
            result.put("status", 1);
            result.put("msg", "添加收货地址成功");
            result.put("shippingId", shippingId);
        } else {
            result.put("status", 0);
            result.put("msg", "添加收货地址失败");
        }
        return result;
    }
    
    /**
     * 删除收货地址
     */
    @PostMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(@RequestParam("shippingId") Integer shippingId, HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        
        // 从session中获取用户信息
        User user = (User) session.getAttribute("user");
        if (user == null) {
            result.put("status", 0);
            result.put("msg", "用户未登录");
            return result;
        }
        
        boolean success = shippingService.delete(user.getId(), shippingId);
        if (success) {
            result.put("status", 1);
            result.put("msg", "删除收货地址成功");
        } else {
            result.put("status", 0);
            result.put("msg", "删除收货地址失败");
        }
        return result;
    }
    
    /**
     * 更新收货地址
     */
    @PostMapping("/update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody Shipping shipping, HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        
        // 从session中获取用户信息
        User user = (User) session.getAttribute("user");
        if (user == null) {
            result.put("status", 0);
            result.put("msg", "用户未登录");
            return result;
        }
        
        boolean success = shippingService.update(user.getId(), shipping);
        if (success) {
            result.put("status", 1);
            result.put("msg", "更新收货地址成功");
        } else {
            result.put("status", 0);
            result.put("msg", "更新收货地址失败");
        }
        return result;
    }
    
    /**
     * 获取收货地址详情
     */
    @GetMapping("/detail")
    @ResponseBody
    public Map<String, Object> detail(@RequestParam("shippingId") Integer shippingId, HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        
        // 从session中获取用户信息
        User user = (User) session.getAttribute("user");
        if (user == null) {
            result.put("status", 0);
            result.put("msg", "用户未登录");
            return result;
        }
        
        Shipping shipping = shippingService.getById(user.getId(), shippingId);
        if (shipping != null) {
            result.put("status", 1);
            result.put("data", shipping);
        } else {
            result.put("status", 0);
            result.put("msg", "地址不存在");
        }
        
        return result;
    }
    
    /**
     * 获取收货地址列表
     */
    @GetMapping("/list")
    @ResponseBody
    public Map<String, Object> list(HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        
        // 从session中获取用户信息
        User user = (User) session.getAttribute("user");
        if (user == null) {
            result.put("status", 0);
            result.put("msg", "用户未登录");
            return result;
        }
        
        result.put("status", 1);
        result.put("data", shippingService.getByUserId(user.getId()));
        
        return result;
    }
    
    /**
     * 设置默认收货地址
     */
    @PostMapping("/set_default")
    @ResponseBody
    public Map<String, Object> setDefault(@RequestParam("shippingId") Integer shippingId, HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        
        // 从session中获取用户信息
        User user = (User) session.getAttribute("user");
        if (user == null) {
            result.put("status", 0);
            result.put("msg", "用户未登录");
            return result;
        }
        
        boolean success = shippingService.setDefault(user.getId(), shippingId);
        if (success) {
            result.put("status", 1);
            result.put("msg", "设置默认地址成功");
        } else {
            result.put("status", 0);
            result.put("msg", "设置默认地址失败");
        }
        return result;
    }
} 